//
//  SendImageView.m
//  供销宝
//
//  Created by poweruser on 2017/7/5.
//  Copyright © 2017年 Stone.Sun. All rights reserved.
//

#import "SendImageView.h"

@interface SendImageView ()

@property (nonatomic,strong) UIView *shadeView;

@property (nonatomic,strong) UIView *backView;

@property (nonatomic,strong) UIButton *cancelButton;

@property (nonatomic,strong) UIButton *confirmButton;

@property (nonatomic,strong) UIImageView *imageView;

@property (nonatomic,strong) UIImageView *verticalView;

@property (nonatomic,strong) UIImageView *horizontalView;

@property (nonatomic,strong) UIImage *image;
@end

@implementation SendImageView

- (instancetype)initWithFrame:(CGRect)frame WithImage:(UIImage *)imageUrl{
    
    if(self == [super initWithFrame:frame]) {
        
        self.image = imageUrl;
        [self fadein];
    }
    return self;
}

- (UIView *)backView {
    
    if(!_backView) {
        
        _backView = [[UIView alloc]initWithFrame:CGRectMake(30,(kScreenHeight-64)/2-180, kScreenWidth-60, 360)];
        
        _backView.backgroundColor = [UIColor whiteColor];
        
        _backView.layer.cornerRadius = 6;
        
        _backView.layer.masksToBounds = YES;
        
        _backView.userInteractionEnabled = YES;
        
        [_backView addSubview:self.imageView];
        
        [_backView addSubview:self.cancelButton];
        
        [_backView addSubview:self.confirmButton];

        [_backView addSubview:self.horizontalView];
        
        [_backView addSubview:self.verticalView];
    }
    return _backView;
}

-(UIView *)shadeView {
    
    if(!_shadeView){
        
        _shadeView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight)];
        
        _shadeView.alpha = 0.3;
        
        _shadeView.backgroundColor = [UIColor blackColor];
    }
    return _shadeView;
}

- (UIImageView *)imageView {
    
    if(!_imageView) {
        
        _imageView = [[UIImageView alloc]initWithFrame:CGRectMake(15, 15, self.backView.frame.size.width-30, self.backView.frame.size.height-80)];
        
        _imageView.image = self.image;
        
        _imageView.layer.borderWidth = 0.5;
        
        _imageView.layer.borderColor = kProgrameBackgroundColor.CGColor;
    }
    return _imageView;
}

- (UIImageView *)horizontalView {
    
    if(!_horizontalView) {
        
        _horizontalView = [[UIImageView alloc]initWithFrame:CGRectMake(0, self.backView.frame.size.height-50, self.backView.frame.size.width, 0.5)];
        
        _horizontalView.backgroundColor = kProgrameBackgroundColor;
        
    }
    return _horizontalView;
}

- (UIImageView *)verticalView {
    
    if(!_verticalView) {
        
        _verticalView = [[UIImageView alloc]initWithFrame:CGRectMake(self.backView.frame.size.width/2, self.backView.frame.size.height-50, 0.5, 50)];
        
        _verticalView.backgroundColor = kProgrameBackgroundColor;
    }
    return _verticalView;
}

- (UIButton *)cancelButton {
    
    if(!_cancelButton) {
        
        _cancelButton = [UIButton buttonWithType:UIButtonTypeCustom];
        
        [_cancelButton setFrame:CGRectMake(0, self.backView.frame.size.height-50, self.backView.frame.size.width/2, 50)];
        
        [_cancelButton setTitle:@"取消" forState:UIControlStateNormal];
        
        [_cancelButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        
        [_cancelButton addTarget:self action:@selector(cancelButtonClick) forControlEvents:UIControlEventTouchUpInside];
    }
    return _cancelButton;
}


- (UIButton *)confirmButton {
    
    if(!_confirmButton) {
        
        _confirmButton = [UIButton buttonWithType:UIButtonTypeCustom];
        
        [_confirmButton setFrame:CGRectMake(self.backView.frame.size.width/2, self.backView.frame.size.height-50, self.backView.frame.size.width/2, 50)];
        
        [_confirmButton setTitle:@"发送" forState:UIControlStateNormal];
        
        [_confirmButton setTitleColor:kProgramColorStyle forState:UIControlStateNormal];
        
        [_confirmButton addTarget:self action:@selector(confirmButtonClick) forControlEvents:UIControlEventTouchUpInside];

    }
    return _confirmButton;
}

//动画出
-(void)fadein {
    
    [[UIApplication sharedApplication].keyWindow addSubview:self.shadeView];
    
    [[UIApplication sharedApplication].keyWindow addSubview:self.backView];
    
//    self.backView.transform = CGAffineTransformMakeScale(.3, .3);
//    [UIView animateWithDuration:.35 animations:^{
//        self.shadeView.alpha = 0.6;
//        self.backView.alpha =1;
//        self.backView.transform = CGAffineTransformMakeScale(1,1);
//    }];
}

//动画缩
-(void)fadeout{

    [UIView animateWithDuration:.1 animations:^{
        self.shadeView.alpha = 0;
        self.backView.alpha=0;
        self.backView.transform = CGAffineTransformMakeScale(.01,.01);
    }completion:^(BOOL finished) {
        
        [self removeFromSuperview];
    }];
}


#pragma mark - action
- (void)cancelButtonClick {
    
    [self fadeout];
}

- (void)confirmButtonClick {
    
    !self.sendImageBlick?:self.sendImageBlick();
    
    [self.backView removeFromSuperview];
    [self.shadeView removeFromSuperview];
    [self removeFromSuperview];
    
}

@end
